package com.xxl.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 返回日期用于返回多少博客，规则是返回一个月前的博客
 * 格式为：xxxx/xx/xx
 * @author xxl
 * @date 2023/4/1
 */
@Slf4j
public class DateUtil {
    /**
     * 返回一个月前的时间 2023/03/15 -> 2023/02/15
     * 2022/10/15
     * 2021/03/15
     * 2023/06/30
     * 2023/07/10
     * 2023/07/15   -->  2023/07/15  --> 2023/06/15
     * @param   ls 是数据库所有博客发布的时间
     * @return String
     */
    public static String getDate(List<String> ls) {
        //拆成三部分  year month day
        Integer[] year = new Integer[1];
        Integer[] month = new Integer[1];
        Integer[] day = new Integer[1];

        //找出最大的年月日
        for (int i = 0; i < ls.size(); i++) {
            String[] split = ls.get(i).split("/");
            int tempYear = Integer.parseInt(split[0]);
            int tempMonth = Integer.parseInt(split[1]);
            int tempDay = Integer.parseInt(split[2]);
            //第一次赋值
            if (i == 0) {
                year[0] = tempYear;
                month[0] = tempMonth;
                day[0] = tempDay;
                continue;
            }
            //如果等于年数相同就进行月数比较
            if (year[0] == tempYear) {
                //如果等于月数相同就进行天数比较
                if (month[0] == tempMonth) {
                    //如果tempDay > day 就把tempDay赋值给day
                    if (day[0] < tempDay) {
                        day[0] = tempDay;
                        continue;
                    }
                }
                if (month[0] < tempMonth) {
                    month[0] = tempMonth;
                    day[0] = tempDay;
                    continue;
                }
            }
            if (year[0] < tempYear) {
                year[0] = tempYear;
                month[0] = tempMonth;
                day[0] = tempDay;
            }
        }

        for (String l : ls) {
            log.info("时间的集合="+l);
        }
        log.info("最终选出的时间为="+year[0]+"/"+month[0]+"/"+day[0]);

        //开始计算一个月前的时间
        Integer needDay = day[0];
        Integer needMonth = month[0];
        Integer needYear = year[0];
        if ((needDay = needDay - 30) <= 0) {
            //到这里代表为负数
            needDay = 30 + needDay;
            if ((needMonth = needMonth - 1) <= 0) {
                //月为12月
                needMonth = 12;
                needYear -= 1;
            }
        }
        log.info("最终计算一个月之前的时间为="+needYear+"/"+needMonth+"/"+needDay);
        //开始组合
        StringBuilder builder = new StringBuilder();
        builder.append(needYear+"/");
        //不符合格式为 xxxx/xx/xx 就补充0
        if (needMonth < 10) {
            builder.append("0"+needMonth);
        }else {
            builder.append(needMonth);
        }
        if (needDay < 10) {
            builder.append("/0" + needDay);
        }else {
            builder.append("/"+needDay);
        }

        return builder.toString();

    }

    public static String getTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        return format.format(new Date());
    }
}
